package com.example.finaltest.fuzzysearch;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.finaltest.MainActivity;
import com.example.finaltest.R;
import com.example.finaltest.goods.GoodsBean;
import com.example.finaltest.goods.GoodsDetailActivity;
import com.example.finaltest.goods.GoodsTypeAdapter;
import com.example.finaltest.goods.TypeBean;
import com.example.finaltest.login.Person_CenterActivity;
import com.example.finaltest.login.UserBean;
import com.example.finaltest.utils.BaseActivity;
import com.example.finaltest.utils.URLUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class FuzzySearchLeaderActivity extends BaseActivity {
    EditText et_search_input;
    ImageView iv_search,back;
    List<GoodsBean.DataBean> data;
    ListView lv_guess_u_like;
 GoodsBean.DataBean dataBean;
    private GoodsTypeAdapter adapter;
    private UserBean.DataBean userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fuzzy_search_leader);//绑定布局
        data = new ArrayList<>();
        initView();//初始化视图
        adapter = new GoodsTypeAdapter(this,data);
        lv_guess_u_like.setAdapter(adapter);
        setListener();//设置监听
//        getNetwork(URLUtils.guessYouLike_url);//启动网络
    }
    private void setListener() {
        iv_search.setOnClickListener(clickListener);
        et_search_input.setOnClickListener(clickListener);
        back.setOnClickListener(clickListener);
        lv_guess_u_like.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GoodsBean.DataBean dataBean = data.get(position);
                Intent intent = new Intent(FuzzySearchLeaderActivity.this, GoodsDetailActivity.class);
                intent.putExtra("goods",dataBean);  //将数据传递给下一级页面
                startActivity(intent);
            }
        });
    }
    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.search_iv:
                    String et_search = et_search_input.getText().toString();
//                    RequestParams params1 = new RequestParams(URLUtils.queryCommodityByName_url+et_search);//get请求拼接
//                    x.http().get(params1, FuzzySearchLeaderActivity.this);
                    getNetwork(URLUtils.queryCommodityByName_url+et_search);
                    break;
                case R.id.search_btn_exit_iv:
              finish();
            }
        }
    };

    @Override
    public void onSuccess(String result) {

        super.onSuccess(result);
       GoodsBean bean = new Gson().fromJson(result, GoodsBean.class);
        List<GoodsBean.DataBean> list = bean.getData();
      data.clear();
//        data = new ArrayList<>();
        data.addAll(list);
        adapter.notifyDataSetChanged();

        //适配器，数据类型不一样自然也不一样的适配器

    }

    private void initView() {
        et_search_input = findViewById(R.id.search_et);
        lv_guess_u_like = findViewById(R.id.lv_search_guess_u_like);
        iv_search = findViewById(R.id.search_iv);
        back= findViewById(R.id.search_btn_exit_iv);
    }
    /*主搜索页面
     * 1.商品模糊查询输入条，求类型get，请求地址/biz/queryCommodityInfo
     * EditText获得文本，请求参数(必须)String name，是模糊查询的字段。
     * 返回参数：(int)code，(String)message，(List<E>)data*/
}